// index page
// ============

define(['require', 'zepto', 'utils', 'sui'], function(require, $, utils) {
  return {
    init: function(data) {
      this.initPage(data.couponBatchId, data.site)
    },
    
    initPage: function(couponBatchId, site) {
      var that = this;
      $(document).on("click", ".button-claim", function() {
        $.modal({
            title: '信息登记',
            text: '<div class="list-block no-margin">' +
                    '<ul>'+
                        '<li>'+
                            '<div class="item-content">'+
                                '<div class="item-inner">'+
                                    '<div class="item-title label">优惠券码</div>'+
                                    '<div class="item-input"><input type="text" name="code" placeholder="请输入您的优惠券码"></div>'+
                                '</div>'+
                            '</div>'+
                        '</li>'+
                        '<li>'+
                            '<div class="item-content">'+
                                '<div class="item-inner">'+
                                    '<div class="item-title label">手机号</div>'+
                                    '<div class="item-input"><input type="text" name="mobile" placeholder="请输入您的手机号"></div>'+
                                '</div>'+
                            '</div>'+
                        '</li>'+
                    '</ul>'+
                '</div>',
            buttons: [
              {
                text: '提交',
                onClick: function () {
                    var code = $("input[name='code']").val().trim(),
                        mobile = $("input[name='mobile']").val().trim();
                    var flag = true;
                     if(!code){
                    	 $.toast('优惠券码不能为空');
                    	 flag=false;
                     }
                     
                     if(flag && (!/^((13)|(15)|(17)|(18))\d{9}$/.test(mobile) || !mobile)){
                         $.toast('请输入正确的手机号');
                         flag = false;
                     }
                     
                     if(flag) {
                    	 var submitUrl = window.__CTX__.root + '/experience-coupons/' + couponBatchId + '/activate';
                         $.post(submitUrl, {
                             'code': code,
                             'mobile': mobile,
                             'site': site,
                             '_method': 'PATCH'
                           }).done(function(response) {
                        	   response = JSON.parse(response)
                               if(response.code == 0) {
				        		    location.href = window.__CTX__.root + response.data.url + '?message=' + response.message;
					           } else {
					        		$.toast(response.message)
					        	}
                           })
                     }
                }
              },
              {
                text: '取消',
                close: true
              }
            ],
            extraClass: 'experience-modal'
        })
      });
      
      //utils.configWeixin(jsConfig, true, that.initShareData);
    }
  }
})